//logs.js
const app = getApp();
const db = wx.cloud.database();
import {
  formatTime
} from '../utils/util'

Page({
  data: {
    id: '',
    logs: [],
    activeIndex: 0,
    dayList: [],
    list: [],
    sum: [{
        title: '今日专注次数',
        val: '0'
      },
      {
        title: '累计专注次数',
        val: '0'
      },
      {
        title: '今日专注时长',
        val: '0分钟'
      },
      {
        title: '累计专注时长',
        val: '0分钟'
      }
    ],
    cateArr: [{
        icon: 'work',
        text: '工作'
      },
      {
        icon: 'study',
        text: '学习'
      },
      {
        icon: 'yvle',
        text: '娱乐'
      }
    ],
  },


  onLoad: function () {

  },

  onShow: function () {
    // this.setData({
    //   logs: (wx.getStorageSync('logs') || []).map(log => {
    //     return util.formatTime(new Date(log))
    //   })
    // })
    var logs = wx.getStorageSync('logs') || [];
    console.log(logs[0])
    var day = 0;
    var total = logs.length;
    var dayTime = 0;
    var totalTime = 0;
    var dayList = [];

    if (logs.length > 0) {
      for (var i = 0; i < logs.length; i++) {
        let a = logs[i].date + ""
        let b = formatTime(new Date) + ""
        console.log(logs[i].date)
        console.log(formatTime(new Date))
        if (a.slice(0, 10) == b.slice(0, 10)) {
          console.log(formatTime(new Date))
          day = day + 1;
          dayTime = dayTime + parseInt(logs[i].time);
          dayList.push(logs[i]);
          this.setData({
            dayList: dayList,
            list: dayList
          })
        }
        totalTime = totalTime + parseInt(logs[i].time);
      }
      this.setData({
        'sum[0].val': day,
        'sum[1].val': total,
        'sum[2].val': dayTime + '分钟',
        'sum[3].val': totalTime + '分钟'
      })
    }


  },
  changeType: function (e) {
    var index = e.currentTarget.dataset.index;
    if (index == 0) {
      this.setData({
        list: this.data.dayList
      })
    } else if (index == 1) {
      var logs = wx.getStorageSync('logs') || [];
      this.setData({
        list: logs
      })
    }
    this.setData({
      activeIndex: index
    })
  },

  logout() {
    wx.reLaunch({
      url: '/pages/login/login',
    })
  }
})